package com._520it.wms.web.controller;

import com._520it.wms.domain.StockIncomeBill;
import com._520it.wms.query.StockIncomeBillQueryObject;
import com._520it.wms.service.IDepotService;
import com._520it.wms.service.IStockIncomeBillService;
import com._520it.wms.util.JSONResult;
import com._520it.wms.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by HASEE on 2017/11/14.
 */
@Controller
@RequestMapping("/stockIncomeBill")
public class StockIncomeBillContorller {
    @Autowired
    private IStockIncomeBillService stockIncomeBillService;
    @Autowired //仓库
    private IDepotService depotService;

    @RequestMapping("/list")
    @RequiredPermission("采购入库订单列表")
    public String list(Model model, @ModelAttribute("qo") StockIncomeBillQueryObject qo) {
        model.addAttribute("depots", depotService.listAll());
        //int a = 1/0; 异常解析器
        model.addAttribute("pageResult", stockIncomeBillService.query(qo));
        return "/stockIncomeBill/list";
    }

    @RequestMapping("/input")
    @RequiredPermission("采购入库订单编辑")
    public String input(Long id, Model model) {
        model.addAttribute("depots", depotService.listAll());
        if (id != null) {
            StockIncomeBill stockIncomeBill = stockIncomeBillService.get(id);
            model.addAttribute("stockIncomeBill", stockIncomeBill);
        }
        return "/stockIncomeBill/input";
    }

    @RequestMapping("/view")
    @RequiredPermission("采购入库订单查看")
    public String view(Long id, Model model) {
        if (id != null) {
            StockIncomeBill stockIncomeBill = stockIncomeBillService.get(id);
            model.addAttribute("stockIncomeBill", stockIncomeBill);
        }
        return "/stockIncomeBill/views";
    }

    @RequestMapping("delete")
    @RequiredPermission("采购入库订单删除")
    @ResponseBody
    public JSONResult delete(Long id) {
        if (id != null) {
            stockIncomeBillService.delete(id);
        }
        return new JSONResult();
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission("采购入库订单新增/更新")
    @ResponseBody
    public JSONResult saveOrUpdate(StockIncomeBill stockIncomeBill) {
        stockIncomeBillService.saveOrUpdate(stockIncomeBill);
        return new JSONResult();
    }

    @RequestMapping("/audit")
    @RequiredPermission("采购入库订单审核")
    @ResponseBody
    public JSONResult audit(Long id) {
        JSONResult jsonResult = new JSONResult();
        try {
            if (id != null) {
                stockIncomeBillService.audit(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

}
